<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="login-page">
    <div class="login-box">
      <h2>欢迎登录</h2>
      <span></span>
      <el-segmented
        v-model="role"
        :options="roleList"
        block
        style="background-color: #fff; width: 100%; height: 40px"
      />
      <span></span>
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="account">
          <el-input v-model="ruleForm.account" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
        </el-form-item>
        <span></span>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm(ruleFormRef)"
            style="width: 100%; height: 40px; font-size: 16px; border-radius: 50px"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      <div class="links">
        <a href="#" @click.prevent="forgotPassword">忘记密码?</a>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()

const ruleFormRef = ref()
const role = ref('学生')

const roleList = ['学生', '老师', '管理员']

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入账号'))
  } else {
    if (ruleForm.checkPass !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPass')
    }
    callback()
  }
}
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
    // } else if (value !== ruleForm.pass) {
    //   callback(new Error("Two inputs don't match!"))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  account: '',
  password: ''
})

const rules = reactive({
  account: [{ validator: validatePass, trigger: 'blur' }],
  password: [{ validator: validatePass2, trigger: 'blur' }]
})

const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      // console.log('submit!')
      // console.log(role.value)
      if (role.value === '学生') {
        console.log('学生登录')
        router.push('/student/')
      } else if (role.value === '老师') {
        console.log('老师登录')
        router.push('/teacher/')
      } else if (role.value === '管理员') {
        console.log('管理员登录')
        router.push('/administrators/')
      }
    } else {
      console.log('error submit!')
    }
  })
}

// const resetForm = (formEl) => {
//   if (!formEl) return
//   formEl.resetFields()
// }
</script>

<style lang="less" scoped>
span {
  margin: 5px;
}
.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(to right, #4747fd, #cbcfff);
}

.login-box {
  background-color: #ffffff;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  text-align: center;
  width: 350px;
}

input[type='text'],
input[type='password'] {
  display: block;
  margin: 1rem auto;
  padding: 0.5rem;
  width: 80%;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 4px;
  color: #fff;
  background-color: #4d4dff;
  cursor: pointer;
}

button:hover {
  background-color: #3333cc;
}

.remember-me {
  text-align: left;
  margin-bottom: 1rem;
}

.links {
  margin-top: 1rem;
}

.links a {
  margin: 0 1rem;
  color: #3333cc;
  text-decoration: none;
}

.links a:hover {
  text-decoration: underline;
}
</style>
